package leetcode;

import java.util.Arrays;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-12-03 22:39
 **/
public class LeetCode1005 {

    //排序
    public int largestSumAfterKNegations(int[] nums, int k) {
        Arrays.sort(nums);
        int len = nums.length;
        int min = Integer.MAX_VALUE;
        int sum = 0;
        for (int i = 0; i < len; i++) {
            if (k > 0 && nums[i] < 0) {
                nums[i] = -nums[i];
                k--;
            }
            sum += nums[i];
            min = Math.min(min, nums[i]);
        }
        return sum - ((k % 2 == 0) ? 0 : 2 * min);
    }

    public static void main(String[] args) {
        LeetCode1005 code = new LeetCode1005();
        System.out.println(code.largestSumAfterKNegations(new int[]{4, 2, 3}, 1));
    }

}
